package ua.org.nuos.sdms.clientgui.client.components.window;

import com.vaadin.ui.*;
import ua.org.nuos.sdms.clientgui.client.components.factory.ButtonFactory;
import ua.org.nuos.sdms.clientgui.client.components.factory.LayoutFactory;
import ua.org.nuos.sdms.clientgui.client.components.factory.WindowFactory;

/**
 * Created by IntelliJ IDEA.
 * User: dio
 * Date: 25.03.12
 * Time: 19:55
 * To change this template use File | Settings | File Templates.
 */
public class CustomDialog extends AbstractWindow {

    private static final String WIDTH = "400";

    private Listener listener;
    private String caption;
    private String text;
    private int labelContentMode;
    private String okCaption;
    private String cancelCaption;

    public CustomDialog(Listener listener, String caption, String text, int labelContext, String okCaption, String cancelCaption) {
        this.listener = listener;
        this.caption = caption;
        this.text = text;
        this.labelContentMode = labelContext;
        this.okCaption = okCaption;
        this.cancelCaption = cancelCaption;

        init();
    }

    public CustomDialog(Listener listener, String caption, String text, String okCaption, String cancelCaption) {
        this.listener = listener;
        this.caption = caption;
        this.text = text;
        this.okCaption = okCaption;
        this.cancelCaption = cancelCaption;

        init();
    }

    private void init() {
        window = WindowFactory.getModalWindow(WIDTH, caption);

        Label context = new Label(text);
        if (labelContentMode > 0) {
            context.setContentMode(labelContentMode);
        }
        Button ok = ButtonFactory.getButton(okCaption, getClickListener(true));
        Button cancel = ButtonFactory.getButton(cancelCaption, getClickListener(false));
        Layout buttons = LayoutFactory.getHLayout(ok, cancel);

        VerticalLayout main = new VerticalLayout();
        main.addComponent(context);
        main.addComponent(buttons);
        main.setComponentAlignment(buttons, Alignment.BOTTOM_RIGHT);

        window.addComponent(main);
    }

    private Button.ClickListener getClickListener(final boolean isConfirm) {
        return new Button.ClickListener() {

            @Override
            public void buttonClick(Button.ClickEvent event) {
                Event e = new Event(isConfirm);
                listener.onClose(e);
                close();
            }
        };
    }

    public class Event {

        private boolean isConfirm;

        public Event(boolean confirm) {
            isConfirm = confirm;
        }

        public boolean isConfirm() {
            return isConfirm;
        }

        public void setConfirm(boolean confirm) {
            isConfirm = confirm;
        }
    }

    public interface Listener {
        void onClose(Event event);
    }
}
